<template>
  <div>
    <div>
      <msg :title="message" :description="description" :buttons="buttons" :icon="result"></msg>
    </div>
  </div>
</template>

<script>
import { Grid, GridItem, Icon, Msg } from "vux";
export default {
  name: "Execution",
  components: {
    Grid,
    GridItem,
    Icon,
    Msg
  },
  data() {
    return {
      result: "waiting",
      message: "正在获取位置中...",
      description: "",
      buttons: [],
      finished: false
    };
  },
  mounted() {
    this.$getUserInfo().then(userInfo => {
      setInterval(() => {
        if (this.finished == false) {
          this.warn("打卡超时，请重试");
        }
      }, 15000);
      this.$register(["getLocation", "closeWindow"])
        .then(() => {
          this.$wechat.getLocation({
            type: "gcj02",
            success: res => {
              // alert("位置获取完成")
              if (res.errMsg === "getLocation:ok") {
                // 上传位置
                this.$http
                  .post("/attendance/api/punch", {
                    sysUser: {
                      id: userInfo.id
                    },
                    wkt: "POINT(" + res.longitude + " " + res.latitude + ")"
                  })
                  .then(res => {
                    this.finished = true;
                    this.result = "success";
                    this.message = "打卡成功";
                    this.showButtons();
                  })
                  .catch(res => {
                    this.warn("打卡错误");
                    this.description = res.data.msg;
                  });
              } else {
                this.warn("未知原因导致获取地理位置失败");
              }
              console.log(res);
            },
            error: res => {
              this.warn("未知原因导致微信获取地理位置失败");
            },
            cancel: function(res) {
              this.warn("由于您拒绝程序获取位置，导致打卡失败。");
            }
          });
        }).catch((msg, error) => {
          //要么注册出错，要么什么出错。。
          this.warn(msg);
          this.description = error;
        });
    });
  },
  methods: {
    toList() {
      this.$router.push({ path: "/attend/list" });
    },
    warn(msg) {
      this.finished = true;
      this.result = "warn";
      this.message = msg;
      this.showButtons();
    },
    showButtons() {
      this.buttons = [
        {
          type: "primary",
          text: "查看我的打卡记录",
          onClick: this.toList
        },
        {
          type: "default",
          text: "关闭网页",
          onClick: this.$wechat.closeWindow
        }
      ];
    }
  }
};
</script>

<!--<style lang="css">-->
  <!--body,html{-->
    <!--height:100%;-->
    <!--width:100%;-->
  <!--}-->

  <!--.testflex{-->
    <!--height:100%;-->
    <!--width:100%;-->
    <!--background-color: gray;-->
    <!--justify-content:center;-->
    <!--align-items:center;-->
    <!--display:-webkit-flex;-->
  <!--}-->
  <!--.testflex1_1{-->
    <!--height:10%;-->
    <!--width:10%;-->
    <!--background-color: blue;-->
  <!--}-->
<!--</style>-->
